home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 2: CDPD 1 / Almathera Ten on Ten - Disc 2: CDPD 1.iso / pd / 176-200 / 179 / unixutil / tail.c < prev    next >
C/C++ Source or Header  |  1995-03-13  |  3KB  |  156 lines

  1. /* tail.c - print the last few lines in a file.  If count is specified, *
  2.  *    that many lines are printed instead of the default value of    *
  3.  *    ten lines.  If no files are specified, head reads standard    *
  4.  *    input.                                *
  5.  *                                    *
  6.  * tail [-<count>] [<file> ...]                     *
  7.  *                                    *
  8.  * tail (C) 1988 by Gary L. Brant                    *
  9.  *                                    *
  10.  * :ts=8                                */
  11.  
  12. #include <stdio.h>
  13. #define   MAXLINE   1000
  14.  
  15. void fputs ();
  16. int default_lines = 10; /* default number of lines to list */
  17. int headlin = 0;
  18.  
  19. typedef struct list {
  20.    struct list *nxt;
  21.    char line[MAXLINE];
  22. };
  23.  
  24.  
  25. main (argc, argv)    /* list 1st n lines of a file */
  26. int  argc;
  27. char *argv[];
  28. {
  29.    FILE *input, *fopen ();
  30.    void fclose ();
  31.    int i = 0, rval;
  32.    long maxlines, convert ();
  33.    char ch;
  34.  
  35.    maxlines = default_lines;
  36.    while (++i < argc) {
  37.       if (argv [i][0] == '-') {     /* process line count argument */
  38.      maxlines = convert (argv[i]);
  39.      default_lines = maxlines;
  40.       } else if (argv [i][0] == '+') {
  41.      maxlines = -convert (argv[i]);
  42.      default_lines = maxlines;
  43.       } else {
  44.      ++headlin;
  45.      if ((input = fopen (argv[i], "r")) == NULL) {
  46.         fputs ("tail: can't open ", stderr);
  47.         fputs (argv[i], stderr);
  48.         putc ('\n', stderr);
  49.         break;
  50.      } else {
  51.         rval = list (input, argv[i], maxlines);
  52.         fclose (input);
  53.         if (rval)
  54.                quit (rval);
  55.      }
  56.       }
  57.    }
  58.    if (!headlin)
  59.       quit (list (stdin, "", maxlines));
  60.    quit (0);
  61. }
  62.  
  63.  
  64. list (fp, fn, maxlines)     /* list tail of a file */
  65. FILE *fp;
  66. char *fn;
  67. register long maxlines;
  68. {
  69.    int n;
  70.    char line[MAXLINE];
  71.    char *c, *fgets (), *alloc ();
  72.  
  73.    if (maxlines < 0) {        /* +n, head relative list */
  74.       while (maxlines++ < 0 && (c = fgets (line, MAXLINE, fp)) != NULL);
  75.       if (c != NULL) {
  76.      heading (fn);
  77.      while ((fgets (line, MAXLINE, fp)) != NULL)
  78.         fputs (line, stdout);
  79.       }
  80.    } else {            /* -n, tail relative list */
  81.       register struct list *head = NULL, *curr, *next, *p;
  82.       register long nlin = 0;
  83.  
  84.       while ((fgets (line, MAXLINE, fp)) != NULL) {
  85.          n = strlen (line);
  86.      if (++nlin > maxlines) {    /* de-link 1st line */
  87.         p = head;
  88.         head = head->nxt;
  89.         freeit (p);
  90.      }
  91.      if ((next = (struct list *) alloc (n+5)) == NULL) {
  92.         fputs ("not enough memory for list\n", stderr);
  93.         while (head != NULL) {
  94.            p = head;
  95.            head = head->nxt;
  96.            freeit (p);
  97.         }
  98.         return (20);
  99.      }
  100.      if (head == NULL)
  101.         head = next;
  102.      else
  103.         curr->nxt = next;
  104.      curr = next;
  105.      strcpy (next->line, line);
  106.      next->nxt = NULL;
  107.       }
  108.       heading (fn);
  109.  
  110.       /* now print the lines in the list, deallocating as we go */
  111.  
  112.       while (head != NULL) {
  113.          fputs (head->line, stdout);
  114.      p = head;
  115.      head = head->nxt;
  116.      freeit (p);
  117.       }
  118.    }
  119.    return (0);
  120. }
  121.  
  122.  
  123. heading (filename)
  124. char *filename;
  125. {
  126.    if (headlin) {
  127.       fputs ("==> ", stdout);
  128.       fputs (filename, stdout);
  129.       fputs (" <==\n", stdout);
  130.    }
  131. }
  132.  
  133.  
  134. long convert (string)
  135. char string[];
  136. {
  137.    register long maxlines = 0;
  138.    register int j;
  139.    register char ch;
  140.  
  141.    for (j = 1; (ch = string[j]) != '\0'; j++)
  142.    if (ch >= '0' && ch <= '9') {
  143.       maxlines *= 10;
  144.       maxlines += ch - '0';
  145.    } else {
  146.       maxlines = default_lines;
  147.       break;
  148.    }
  149.    return (maxlines);
  150. }
  151.  
  152.  
  153. void _wb_parse ()
  154. {
  155. }
  156.